package controller.link.game;

import view.client.Console;
import view.client.PanelGame;
import controller.Link;
import controller.link.RefreshGameData;
import java.io.Serializable;
import model.game.Player;
import model.Attendant;
import model.Server;

public class Healing implements Link, Serializable {

    private Player player;
    private boolean complete;
    private int damage;
    private int begin;
    private int end;
    private boolean clientExecuting;
    private String name;

    public Healing(Player player, String name) {
        this.name = name;
        this.player = player;
        this.damage = player.getTotalLife() - player.getLife();
        this.begin = 0;
        this.end = damage;
        this.clientExecuting = false;
    }

    public boolean notComplete() {
        return !complete;
    }

    @Override
    public void serverUse(Server server, Attendant at) {
        player.setLife(player.getLife() + 1);
        if (player.getLife() >= player.getTotalLife()) {
            complete = true;
            RefreshGameData rgd = new RefreshGameData();
            rgd.serverUse(server, at);
        } else {
            complete = false;           
        }
        at.send((Link) this);
    }

    @Override
    public void clientUse(Console console) {
        PanelGame pg = console.getPg();
        if (complete) {
            pg.getTxtArea().append("<<Você está curado>>\n");
            if (console.getDh()!=null){
                console.closeDialog();
            }          
        } else {
            if (isClientExecuting()) {
                console.getDh().getLblProgress().setText(name);
                console.getDh().getPbProgress().setStringPainted(true);
                console.getDh().getPbProgress().setMaximum(player.getTotalLife());
                console.getDh().getPbProgress().setStringPainted(true);
                console.getDh().getPbProgress().setValue(player.getLife());
                console.getDh().getPbProgress().repaint();
            } else if (!isClientExecuting()) {
                console.initDialog();
            }
        }

    }

    public boolean isClientExecuting() {
        return clientExecuting;
    }

    public void setClientExecuting(boolean clientExecuting) {
        this.clientExecuting = clientExecuting;
    }
}
